package com.android.lovely.room

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.android.lovely.data.Bot
import com.android.lovely.data.CardItem
import com.android.lovely.room.BotCacheService.convertToUniqueId
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

@Entity(tableName = "Profile")
@JsonClass(generateAdapter = true)
data class CacheProfile(
    @PrimaryKey(autoGenerate = false) @ColumnInfo(name = "cache_id") @Json(name = "cache_id") val cacheId: String = "",
    @ColumnInfo(name = "avatar") @Json(name = "avatar") var avatar: String = "",
    @ColumnInfo(name = "id") @Json(name = "id") val id: Int = 0,
    @ColumnInfo(name = "intimacy_value") @Json(name = "intimacy_value") var intimacyValue: Int = 0,
    @ColumnInfo(name = "can_get_image") @Json(name = "can_get_image") var canGetImage: Boolean = false,
    @ColumnInfo(name = "scenario") @Json(name = "scenario") var scenario: String = "",
    @ColumnInfo(name = "name") @Json(name = "name") var name: String = "",
    @ColumnInfo(name = "card_num") @Json(name = "card_num") var cardNum: Int = 0,
    @ColumnInfo(name = "personality") @Json(name = "personality") var personality: String = "",
    @ColumnInfo(name = "point") @Json(name = "point") var point: Int = 0,
    @ColumnInfo(name = "gender") @Json(name = "gender") var gender: Int = 0,
    @ColumnInfo(name = "is_collect") @Json(name = "is_collect") var isCollect: Int = 0,
    @ColumnInfo(name = "background_img") @Json(name = "background_img") var background: String = "",
    @ColumnInfo(name = "chat_mode") @Json(name = "chat_mode") var hasChatMode: Boolean = false,
    @ColumnInfo(name = "chat_name") @Json(name = "chat_name") var chatName: String = "",
    @ColumnInfo(name = "chat_gender") @Json(name = "chat_gender") var chatGender: Int = 1,
    @ColumnInfo(name = "chat_dec") @Json(name = "chat_dec") var chatDec: String = "",
    @ColumnInfo(name = "user_name") @Json(name = "user_name") var userName: String = "",
    @ColumnInfo(name = "user_id") @Json(name = "user_id") var userId: Int = 0,
    @ColumnInfo(name = "tag_list") @Json(name = "tag_list") var roleTag: String = "",
    @ColumnInfo(name = "card_list") @Json(name = "card_list") var cards: List<CardItem> = emptyList(),
)

fun Bot.toCache() = CacheProfile(
    id.convertToUniqueId(),
    avatar["url"].orEmpty(),
    id,
    intimacyValue,
    canGetImage,
    scenario,
    name,
    cardNum,
    personality,
    point,
    gender,
    isCollect,
    backgroundImg,
    chatMode.isNullOrEmpty().not(),
    chatName,
    chatGender,
    chatDec,
    userInfo["name"].orEmpty(),
    userInfo["id"]?.toIntOrNull() ?: 0,
    roleTag.joinToString { it["tag_name"].orEmpty() }
)